package com.prs.crm.dao.stock.impl;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.stock.StoreOutDao;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreOut;
import com.prs.crm.domain.store.StoreRoom;

public class StoreOutDaoImpl extends BaseDaoImpl<StoreOut> implements
		StoreOutDao {

	@SuppressWarnings("unchecked")
	public ProductStore getStore(final StoreRoom room,final Product product)
	{
		return (ProductStore )getHibernateTemplate().execute(   
			      new HibernateCallback() {   
			        public Object doInHibernate(Session session) throws HibernateException {   
			        	Criteria criteria =  session.createCriteria(ProductStore.class);
			        	criteria.add(Restrictions.eq("room", room))
			        		.add(Restrictions.eq("product", product));
			        	List<ProductStore> result = criteria.list();
			        	if (result.size()>0)
			        		return result.get(0);
			        	else
			        		return null;
			        }   
			      }   
			    );  
	}

}
